package com.ys.service.hr.impl;

import com.alibaba.fastjson.JSON;
import com.ys.dto.AddDto;
import com.ys.entity.hr.ThrOperateCompanyBankInfo;
import com.ys.entity.sys.YsCol;
import com.ys.mapper.hr.ThrOperateCompanyBankInfoMapper;
import com.ys.service.hr.ThrOperateCompanyBankInfoService;
import com.ys.util.CheckUtil;
import com.ys.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;
import java.util.Map;

@Service("thrOperateCompanyBankInfoService")
public class ThrOperateCompanyBankInfoServiceImpl implements ThrOperateCompanyBankInfoService {

    @Autowired
    private ThrOperateCompanyBankInfoMapper thrOperateCompanyBankInfoMapper;

    @Autowired
    private CheckUtil checkUtil;


    @Override
    public R add(Map<String, Object> params) throws Exception {
        //验证
        String msg = yzMsg(params);
        if (msg != null){
            return R.error(msg);
        }
        ThrOperateCompanyBankInfo thrOperateCompanyBankInfo = JSON.parseObject(JSON.toJSONString(params), ThrOperateCompanyBankInfo.class);
        if (new ThrOperateCompanyBankInfo().setVid(thrOperateCompanyBankInfo.getVid()).count() != 0){
            return R.error("该账户编码已存在，核实后再操作!");
        }
        if (new ThrOperateCompanyBankInfo().setVyhzh(thrOperateCompanyBankInfo.getVyhzh()).setVgsbm(thrOperateCompanyBankInfo.getVgsbm()).setStatus(1).count() != 0 ) {
            return R.error("该银行账户编码和运营公司已存在，核实后再操作!");
        }
        if (!thrOperateCompanyBankInfo.getVyhzh().matches("\\d+(\\.\\d{0,0})?")){
            return R.error("银行账户编码需要填写数字！");
        }
        if (!thrOperateCompanyBankInfo.getVkhh().matches("^[a-zA-Z\u4e00-\u9fa5]*$")){
            return R.error("开户行需要填写字符！");
        }
        if (!thrOperateCompanyBankInfo.getVyhzhmc().matches("^[a-zA-Z\u4e00-\u9fa5]*$")){
            return R.error("银行账户名称需要填写字符！");
        }
        thrOperateCompanyBankInfoMapper.insert(thrOperateCompanyBankInfo);
        return R.ok(AddDto.gotoEdit("/web/sys/thrOperateCompanyBankInfo/toEdit?id=" + thrOperateCompanyBankInfo.getId()));
    }

    /**
     * 修改公司银行信息
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    public R edit(Map<String, Object> params) throws Exception {

        //验证
        String msg = yzMsg(params);
        if (msg != null){
            return R.error(msg);
        }
        ThrOperateCompanyBankInfo thrOperateCompanyBankInfo = JSON.parseObject(JSON.toJSONString(params), ThrOperateCompanyBankInfo.class);
        if (new ThrOperateCompanyBankInfo().setVid(thrOperateCompanyBankInfo.getVid()).where("A.id != " + thrOperateCompanyBankInfo.getId()).count() != 0){
            return R.error("该账户编码已存在，核实后再操作!");
        }
        if (new ThrOperateCompanyBankInfo().setVyhzh(thrOperateCompanyBankInfo.getVyhzh()).setVgsbm(thrOperateCompanyBankInfo.getVgsbm()).setStatus(1).where("A.id != " + thrOperateCompanyBankInfo.getId()).count() != 0){
            return R.error("该银行账户编码和运营公司已存在，核实后再操作!");
        }
        if (!thrOperateCompanyBankInfo.getVyhzh().matches("\\d+(\\.\\d{0,0})?")){
            return R.error("银行账户编码需要填写数字！");
        }
        if (!thrOperateCompanyBankInfo.getVkhh().matches("^[a-zA-Z\u4e00-\u9fa5]*$")){
            return R.error("开户行需要填写字符！");
        }
        if (!thrOperateCompanyBankInfo.getVyhzhmc().matches("^[a-zA-Z\u4e00-\u9fa5（）()]*$")){
            return R.error("银行账户名称需要填写字符！");
        }
        thrOperateCompanyBankInfoMapper.update(thrOperateCompanyBankInfo);
        return R.ok(AddDto.gotoEdit("/web/sys/thrOperateCompanyBankInfo/toEdit?id=" + thrOperateCompanyBankInfo.getId()));

    }

    private String yzMsg(Map<String, Object> infoMap){
        //验证导入字段
        String backMsg = "";
        //获取pc字段数据
        List<YsCol> listField = new YsCol()
                .where("A.MenuId = "+10247L)
                .setOrderby("A.SortNo ASC")
                .queryList();
        //验证必填字段的非空；其他字段的长度限制、类型
        try {
            backMsg = checkUtil.checkTableColLengthAndDataType(
                    "THR_OperateCompanyBankInfo",
                    listField,
                    infoMap,
                    ysColItem-> !"id".equals(ysColItem.getCode()) && !"vbz".equals(ysColItem.getCode())
                            && !"createTime".equals(ysColItem.getCode())&& !"attachment".equals(ysColItem.getCode())
                            && !"vyhzhmc".equals(ysColItem.getCode())&& !"vyhzh".equals(ysColItem.getCode())
                            && !"vkhh".equals(ysColItem.getCode()) && !"cUser".equals(ysColItem.getCode())
                            && !"vlhh".equals(ysColItem.getCode())

            );
            if(backMsg!=null && !"".equals(backMsg)){//若有误，则跳过这条信息
                return backMsg;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
